package com.innovation.chengzhangriji.ui.widget;

import android.content.Context;
import android.graphics.Point;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.R;

public class TimelineAudioView extends RelativeLayout implements View.OnClickListener, View.OnLongClickListener {

    private static final String SECOND = "''";

    public interface OnClickAudioListener {
        void onClickAudio();

        void onLongClickAudio();
    }

    private FrameLayout frameLayout;

    private ImageView mImageAudio;

    private CuteTextView mTextLength;

    private OnClickAudioListener mOnClickAudioListener;

    public TimelineAudioView(Context context) {
        super(context);
        init(context);
    }

    public TimelineAudioView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public TimelineAudioView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @Override
    public void onClick(View v) {
        if (mOnClickAudioListener == null) {
            return;
        }
        if (v.getId() == R.id.layout_audio) {
            mOnClickAudioListener.onClickAudio();
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if (mOnClickAudioListener == null) {
            return false;
        }
        if (v.getId() == R.id.layout_audio) {
            mOnClickAudioListener.onLongClickAudio();
            return true;
        }
        return false;
    }

    public void setLength(int length) {
        mTextLength.setText(length + SECOND);
        setLength(1.0f * length / Config.AUDIO_MAX_LENGTH);
    }

    public void play() {
        final AnimationDrawable drawable = (AnimationDrawable) getContext().getResources().getDrawable(R.drawable.anim_record);
        mImageAudio.setImageDrawable(drawable);
        mImageAudio.post(new Runnable() {
            @Override
            public void run() {
                if (drawable != null) {
                    drawable.start();
                }
            }
        });
    }

    public void stop() {
        mImageAudio.setImageDrawable(getContext().getResources().getDrawable(R.drawable.ic_record_3));
    }

    public void ready() {
        mImageAudio.setVisibility(View.VISIBLE);
    }

    public void notReady() {
        mImageAudio.setVisibility(View.INVISIBLE);
    }

    public void setOnClickAudioListener(OnClickAudioListener onClickAudioListener) {
        mOnClickAudioListener = onClickAudioListener;
    }

    private void init(Context context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.layout_timeline_audio, this);
        frameLayout = (FrameLayout) findViewById(R.id.layout_audio);
        frameLayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_card));
        float density = getResources().getDisplayMetrics().density;
        int padL = (int) (16 * density);
        int padR = (int) (14 * density);
        int padY = (int) (12 * density);
        frameLayout.setPadding(padL, padY, padR, padY);
        mImageAudio = (ImageView) findViewById(R.id.image_audio);
        mTextLength = (CuteTextView) findViewById(R.id.text_length);
        frameLayout.setOnClickListener(this);
        frameLayout.setOnLongClickListener(this);
    }

    private void setLength(float scale) {
        final int defaultWidth = (int) (getContext().getResources().getDisplayMetrics().density * 72);
        if (scale <= 0 || scale > 1) {
            frameLayout.setLayoutParams(new RelativeLayout.LayoutParams(defaultWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
            return;
        }
        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        Point point = new Point();
        wm.getDefaultDisplay().getSize(point);
        float width = defaultWidth;
        float width2 = point.x - getContext().getResources().getDisplayMetrics().density * (72) - defaultWidth;
        width = width + width2 * scale;
        frameLayout.setLayoutParams(new RelativeLayout.LayoutParams((int) (width), ViewGroup.LayoutParams.WRAP_CONTENT));
    }

}
